package com.zc.framework.web.tablib;

import com.zc.framework.system.SessionUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.util.TagUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * Created by zc on 2016/11/30.
 */
public class SessionGetTag extends TagSupport {

    private static Logger logger = LogManager.getLogger(SessionGetTag.class);

    protected String id;
    protected String toScope = null;
    protected String subsystem = "";
    protected String module = "";
    protected String page = "";
    protected String key;

    public SessionGetTag() {
    }

    @Override
    public int doStartTag() throws JspException {
        if(this.id != null && this.key != null) {
            int inScope1 = 1;

            if(this.toScope != null) {
                inScope1 = TagUtils.getScope(this.toScope);
            }

            Object value = null;
            HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();

            value = SessionUtil.getAttribute(request, this.subsystem, this.module, this.page, this.key);

            if(value != null) {
                this.pageContext.setAttribute(this.id, value, inScope1);
            }

            return 0;
        } else {
            JspException inScope = new JspException("define.null");
            logger.error(inScope.getMessage(),inScope);
        }
        return 0;
    }



    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    public String getToScope() {
        return toScope;
    }

    public void setToScope(String toScope) {
        this.toScope = toScope;
    }

    public String getSubsystem() {
        return subsystem;
    }

    public void setSubsystem(String subsystem) {
        this.subsystem = subsystem;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}
